﻿<%@ WebHandler Language="C#" Class="UploadPhotoImgs" %>
/**
 * 图片中心批量上传 
 *
 */
using System;
using System.Collections;
using System.Web;
using System.IO;
using System.Globalization;
using Ajeelee.Core.Utils;
using AiLuWeb.Model;
using AiLuWeb.Business;

public class UploadPhotoImgs : IHttpHandler
{
    private HttpContext context;

    public void ProcessRequest(HttpContext context)
    {
        context.Request.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        
        string cid = context.Request.Form["cid"];
        string username = context.Request.Form["uname"];
        if (cid.ToInt() <= 0)
        {
            showError("需要上传图集ID不存在，请重新打开页面。");
        }
        //String aspxUrl = context.Request.Path.Substring(0, context.Request.Path.LastIndexOf("/") + 1);
        String aspxUrl = "";

        //文件保存目录路径
        String savePath = "/upfile/";

        //文件保存目录URL
        String saveUrl = aspxUrl + "/upfile/";

        //定义允许上传的文件扩展名
        Hashtable extTable = new Hashtable();
        extTable.Add("image", "gif,jpg,jpeg,png,bmp");

        //最大文件大小
        int maxSize = 10000000;// 10MB
        this.context = context;

        HttpPostedFile imgFile = context.Request.Files["Filedata"];
        if (imgFile == null)
        {
            showError("请选择文件。");
        }

        String dirPath = context.Server.MapPath(savePath);
        if (!Directory.Exists(dirPath))
        {
            showError("上传目录不存在。");
        }

        String dirName = context.Request.QueryString["dir"];
        if (String.IsNullOrEmpty(dirName))
        {
            dirName = "image";
        }
        if (!extTable.ContainsKey(dirName))
        {
            showError("目录名不正确。");
        }

        String fileName = imgFile.FileName;
        String fileExt = Path.GetExtension(fileName).ToLower();

        if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
        {
            showError("上传文件大小超过限制。");
        }

        if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
        {
            showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
        }

        //创建文件夹
        dirPath += dirName + "/";
        saveUrl += dirName + "/";
        if (!Directory.Exists(dirPath))
        {
            Directory.CreateDirectory(dirPath);
        }
        String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
        dirPath += ymd + "/";
        saveUrl += ymd + "/";
        if (!Directory.Exists(dirPath))
        {
            Directory.CreateDirectory(dirPath);
        }

        String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
        String filePath = dirPath + newFileName;

        imgFile.SaveAs(filePath);

        String fileUrl = saveUrl + newFileName;
        String ThumbnailUrl = saveUrl + "Thum" + newFileName;

        #region 缩略图
        // 120*90   200*150
        AiLuWeb.WebSite.Code.Utility.MakeThumbnail(fileUrl, ThumbnailUrl, 200, 150, AiLuWeb.WebSite.Code.Utility.ThumMode.AutoZoomIn);

        #endregion

        try
        {
            IPhotoImgsBusiness newsBll = new PhotoImgsBusinessFactory().CreatePhotoImgsBusiness();
            PhotoImgs photo = new PhotoImgs();
            photo.Summary = fileName.Replace(fileExt, "");
            photo.CId = cid.ToInt();
            photo.SmallSrc = ThumbnailUrl;
            photo.BigSrc = fileUrl;
            photo.Sort = 0;
            photo.LinkUrl = "";
            photo.InDate = DateTime.Now;
            photo.InUser = username ?? "";
            newsBll.Insert(photo);
        }
        catch (Exception ex)
        {
            Function.WriteErrorLog(ex);
            showError("上传文件时出错！");
        }
        Hashtable hash = new Hashtable();
        hash["error"] = 0;
        hash["url"] = fileUrl;
        if (context != null)
        {
            context.Response.AddHeader("Content-Type", "text/html; charset=GBK");
            context.Response.Write(hash.SerializeJson());
            context.Response.End();
        }
    }

    private void showError(string message)
    {
        Hashtable hash = new Hashtable();
        hash["error"] = 1;
        hash["message"] = message; if (context != null)
        {
            context.Response.AddHeader("Content-Type", "text/html; charset=GBK");
            context.Response.Write(hash.SerializeJson());
            context.Response.End();
        }
    }

    public bool IsReusable
    {
        get
        {
            return true;
        }
    }
}
